package com.lz.nacos.project.controller;


import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.StopWatch;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSON;
import com.lz.nacos.common.domain.ResponseVo;
import com.lz.nacos.common.redis.*;
import com.lz.nacos.project.entity.RedisMonitor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *
 */
@RestController
@RequestMapping(value = "/redis")
public class RedisController {

    @Resource
    private RedisCache redisCache;

    @Resource
    private RedisHashCache redisHashCache;

    @Resource
    private RedisListCache redisListCache;

    @Resource
    private RedisSetCache redisSetCache;

    @Resource
    private RedisSortSetCache redisSortSetCache;

    @PostConstruct
    public void init() {
        send();
    }

    @PostMapping("/send")
    public ResponseVo send() {
        redisString();
        redisHash();
        redisList();
        redisSet();
        redisSortSet();
        return ResponseVo.success();
    }

    /**
     * String
     */
    public ResponseVo redisString() {
        System.out.println("**********************redisString开始*************************");
        StopWatch s1 = new StopWatch();
        s1.start();
        redisCache.setCacheObject("logSwitch1", "1");
        redisCache.setCacheObject("logSwitch2", "1");
        redisCache.setCacheObject("logSwitch3", "1");
        redisCache.setCacheObject("logSwitch4", "1");
        redisCache.setCacheObject("logSwitch5", "1");
        redisCache.setCacheObject("logSwitch6", "1");
        redisCache.setCacheObject("logSwitch7", "1");
        redisCache.setCacheObject("logSwitch8", "1");
        redisCache.setCacheObject("batchSize", "100");
        redisCache.setCacheObject("logoPath", "C://");
        s1.stop();
        System.out.println("存：s1.getTotalTimeMillis():" + s1.getTotalTimeMillis());
        StopWatch sw = new StopWatch();
        sw.start();
        String logSwitch1 = redisCache.getCacheObject("logSwitch1");
        String logSwitch2 = redisCache.getCacheObject("logSwitch2");
        String logSwitch3 = redisCache.getCacheObject("logSwitch3");
        String batchSize = redisCache.getCacheObject("batchSize");
        String logoPath = redisCache.getCacheObject("logoPath");
        sw.stop();
        System.out.println("取：sw.getTotalTimeMillis():" + sw.getLastTaskTimeMillis());

        System.out.println(" sw.getTotalTimeMillis():" + sw.prettyPrint());
        System.out.println("logSwitch1:" + logSwitch1);
        System.out.println("batchSize:" + batchSize);
        System.out.println("logoPath:" + logoPath);
        System.out.println("**********************redisString结束*************************");
        return ResponseVo.success();
    }

    /**
     * Hash
     */
    public ResponseVo redisHash() {
        System.out.println("**********************redisHash开始*************************");
        StopWatch s1 = new StopWatch();
        s1.start();
        redisHashCache.putHash("otcOptionSwitch", "type", "otcOption");
        redisHashCache.putHash("otcOptionSwitch", "saveSwitch", "1");
        redisHashCache.putHash("otcOptionSwitch", "printSwitch", "1");
        redisHashCache.putHash("otcOptionSwitch", "sheetSwitch", "0");
        redisHashCache.putHash("otcOptionSwitch", "calcTimeout", "100");

        redisHashCache.putHash("irOptionSwitch", "type", "irOption");
        redisHashCache.putHash("irOptionSwitch", "saveSwitch", "0");
        redisHashCache.putHash("irOptionSwitch", "printSwitch", "1");
        redisHashCache.putHash("irOptionSwitch", "sheetSwitch", "2");
        redisHashCache.putHash("irOptionSwitch", "calcTimeout", "300");

        s1.stop();
        System.out.println("hash存：s1.getTotalTimeMillis():" + s1.getTotalTimeMillis());

        StopWatch s2 = new StopWatch();
        s2.start();
        Map<Object, Object> map = redisHashCache.getAllHash("otcOptionSwitch");
        s2.stop();
        System.out.println("hash取：s2.getTotalTimeMillis():" + s2.getTotalTimeMillis());
        System.out.println(JSONUtil.toJsonStr(map));
        Map<Object, Object> map2 = redisHashCache.getAllHash("irOptionSwitch");
        System.out.println(JSONUtil.toJsonStr(map2));
        System.out.println("**********************redisHash结束*************************");

        return ResponseVo.success();
    }

    /**
     * List
     */
    public ResponseVo redisList() {
        System.out.println("**********************redisList开始*************************");

        redisCache.deleteObject("redisMonitor");
        RedisMonitor redisMonitor;
        List<Object> list;
        redisMonitor = new RedisMonitor("1", BigDecimal.valueOf(20.121), BigDecimal.valueOf(90.21212));
        //增加 从左侧
        redisListCache.addLeftPushList("redisMonitor", JSON.toJSONString(redisMonitor));
        redisMonitor = new RedisMonitor("2", BigDecimal.valueOf(22.121), BigDecimal.valueOf(30.21212));
        redisListCache.addLeftPushList("redisMonitor", JSON.toJSONString(redisMonitor));
        redisMonitor = new RedisMonitor("3", BigDecimal.valueOf(11.121), BigDecimal.valueOf(40.21212));
        redisListCache.addLeftPushList("redisMonitor", JSON.toJSONString(redisMonitor));

        //获取全部
        list = redisListCache.getList("redisMonitor");
        System.out.println(Convert.toStr(list));
        //获取部分
        list = redisListCache.rangeList("redisMonitor", 1, 2);
        System.out.println(Convert.toStr(list));

        //指定删除
        redisListCache.rightPopList("redisMonitor");

        //获取全部
        list = redisListCache.getList("redisMonitor");
        System.out.println(Convert.toStr(list));
        //获取部分
        list = redisListCache.rangeList("redisMonitor", 0, 1);
        System.out.println(Convert.toStr(list));

        redisCache.deleteObject("redisMonitorCalc");
        List<Object> lt = new ArrayList<>();
        lt.add("calc001");
        lt.add("calc002");
        lt.add("calc003");
        lt.add("calc004");
        lt.add("calc005");
        //增加 从左侧
        redisListCache.addLeftPushListAll("redisMonitorCalc", lt);
        //获取全部
        List<Object> lt2 = redisListCache.getList("redisMonitorCalc");
        System.out.println(Convert.toStr(lt2));

        List<Object> l3 = redisListCache.rangeList("redisMonitorCalc", 0, -1);
        System.out.println(Convert.toStr(l3));
        System.out.println("**********************redisList结束*************************");

        return ResponseVo.success();
    }

    /**
     * Set
     */
    public ResponseVo redisSet() {
        System.out.println("**********************redisSet开始*************************");

        redisSetCache.addSet("online", "calypso");
        redisSetCache.addSet("online", "lz");
        redisSetCache.addSet("online", "lz");
        Set<Object> set = redisSetCache.getSet("online");
        System.out.println(Convert.toStr(set));
        System.out.println("**********************redisSet结束*************************");

        return ResponseVo.success();
    }

    /**
     * SortSet
     */
    public ResponseVo redisSortSet() {

        redisSortSetCache.addSet("pCpu", "p1Cpu", 12);
        redisSortSetCache.addSet("pCpu", "p2Cpu", 80);
        redisSortSetCache.addSet("pCpu", "p3Cpu", 70);
        redisSortSetCache.addSet("pCpu", "p4Cpu", 35);
        redisSortSetCache.addSet("pCpu", "p5Cpu", 60);

        Set set = redisSortSetCache.range("pCpu");
        System.out.println(Convert.toStr(set));

        Set set1 = redisSortSetCache.reverseRangeByScore("pCpu",0,100);
        System.out.println(Convert.toStr(set1));

        return ResponseVo.success();
    }

}